package com.wjk.kylin.rpc.core.netty;

import com.wjk.kylin.rpc.core.netty.params.BaseCallback;
import com.wjk.kylin.rpc.core.provider.RpcProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * server
 * @author wjk
 */

public abstract class AbstractServer {
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractServer.class);

    private BaseCallback startCallback;
    private BaseCallback stopCallback;

    public void setStartCallback(BaseCallback startCallback) {
        this.startCallback = startCallback;
    }

    public void setStopCallback(BaseCallback stopCallback) {
        this.stopCallback = stopCallback;
    }


    /**
     * start server
     *
     * @param providerFactory
     * @throws Exception
     */
    public abstract void start(RpcProviderFactory providerFactory) throws Exception;

    /**
     * callback when started
     */
    public void onStart() {
        if (startCallback != null) {
            try {
                startCallback.execute();
            } catch (Exception e) {
                LOGGER.error(">>>>>>>>>>> kylin-rpc, server startCallback error.", e);
            }
        }
    }

    /**
     * stop server
     *
     * @throws Exception
     */
    public abstract void stop() throws Exception;

    /**
     * callback when stop
     */
    public void onStop() {
        if (stopCallback != null) {
            try {
                stopCallback.execute();
            } catch (Exception e) {
                LOGGER.error(">>>>>>>>>>> kylin-rpc, server stopCallback error.", e);
            }
        }
    }

}
